package com.xl.web.service.zoom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xl.base.annotation.DataSource;
import com.xl.base.page.Page;
import com.xl.base.page.PageBean;
import com.xl.core.enums.SourceEnum;
import com.xl.web.dao.zoom.GroupDao;
import com.xl.web.entity.zoom.Group;
@Service(value = "groupService")
public class GroupService<T extends Group> {
	@Resource
	private GroupDao<Group> groupDao;
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public PageBean queryPageGroupUser(Group group,Page page)throws Exception {
		 PageHelper.startPage(page.getPageNum() ,page.getPageSize());
		 List<Group> groups = groupDao.queryGroupUser(group);
		 PageBean  pageBean  = new PageBean(groups);
		 return pageBean;
	}
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public List<Group> queryGroupUser(Group group)throws Exception {
		return groupDao.queryGroupUser(group);
	}
	
	public void saveGroup(Group group)throws Exception{
		groupDao.saveGroup(group);
	}
	public void delGroup(Group group)throws Exception{
		groupDao.delGroup(group);
	}
	
	public void editGroup(Group group)throws Exception{
		groupDao.editGroup(group);
	}
	
	/**
	 * 用户组添加成员
	 * @param group
	 */
	public void saveGroupUser(JSONObject param)throws Exception{
		JSONArray userList = param.getJSONArray("userIdList");
		    Map paraMap = new HashMap();
			paraMap.put("groupId", param.get("groupId")); 
			 for(int i=0;i<userList.size();i++){
					paraMap.put("userId", userList.get(i));
					int cnt = groupDao.queryGroupUserCnt(paraMap);
					if(cnt==0){
					    groupDao.saveGroupUser(paraMap);
					}
			 }
	}
	
	/**
	 * 删除组中成员
	 * @param group
	 */
	public void delGroupUser(JSONObject param)throws Exception{
		JSONArray userList = new JSONArray();
		    userList = param.getJSONArray("userIdList");
		    Map paraMap = new HashMap();
			paraMap.put("groupId", param.get("groupId")); 
			System.out.println(paraMap.toString());
			if(param.containsKey("userIdList")&&param.getJSONArray("userIdList").size()>0){
				 for(int i=0;i<userList.size();i++){
						paraMap.put("userId", userList.get(i));
						groupDao.delGroupUser(paraMap);
				 }
			}else{
				groupDao.delGroupUser(paraMap);
			}
	}
}
